﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace VBoxService
{
    public class Settings : Object
    {
        string _VBoxManagePath;
        /// <summary>
        /// Path to 'VBoxManage.exe'.
        /// </summary>
        public string VBoxManagePath { get { return _VBoxManagePath; } }

        string _VBoxGuestName;
        /// <summary>
        /// Name of the Guest-System in VirtualBox.
        /// </summary>
        public string VBoxGuestName { get { return _VBoxGuestName; } }

        string _ControlPort;
        /// <summary>
        /// Port to use for Monitor-Controll-Connections.
        /// </summary>
        public string ControlPort { get { return _ControlPort; } }

        string _ODDController;
        /// <summary>
        /// Name of the Optical-Disc-Drive-Controller.
        /// </summary>
        public string ODDController { get { return _ODDController; } }

        string _ODDPort;
        /// <summary>
        /// Port on the ODD-Controller used for DVD-Drive.
        /// </summary>
        public string ODDPort { get { return _ODDPort; } }

        string _ODDDevice;
        /// <summary>
        /// Device-No. on the ODD-Controller-Port used for DVD-Drive.
        /// </summary>
        public string ODDDevice { get { return _ODDDevice; } }

        string _HDDController;
        /// <summary>
        /// Name of the Harddisk-Controller.
        /// </summary>
        public string HDDController { get { return _HDDController; } }

        int _UpdateInterval;
        /// <summary>
        /// Monitor-Update-Interval in Minutes.
        /// </summary>
        public int UpdateInterval { get { return _UpdateInterval; } }

        /// <summary>
        /// Eventlog-Handler.
        /// </summary>
        private System.Diagnostics.EventLog oServiceEvents;

        /// <summary>
        /// Initialize new Settings()-Object.
        /// </summary>
        public Settings()
        {
            // Setup Logging
            oServiceEvents = new System.Diagnostics.EventLog();
            oServiceEvents.Source = "VBoxServiceSource";
            oServiceEvents.Log = "VBoxService";

            // Setup vars.
            _VBoxGuestName = "null";
            _VBoxManagePath = "null";
        }

        /// <summary>
        /// Load Values from 'config.xml'.
        /// </summary>
        /// <returns></returns>
        public bool Load()
        {
            _VBoxGuestName = "null";
            _VBoxManagePath = "null";
            _ControlPort = "8000";
            _ODDController = "IDE-Controler";
            _ODDPort = "0";
            _ODDDevice = "0";
            _HDDController = "SATA-Controler";
            _UpdateInterval = 15;

            try
            {
                XmlDocument xdSettings = new XmlDocument();
                xdSettings.Load(getServiceBaseDir() + "config.xml");

                XmlElement xeDocumentNode = xdSettings.DocumentElement;
                XmlNode xnVBoxManagePath = xeDocumentNode.SelectSingleNode("//VBoxManagePath");
                XmlNode xnVirtualMachineName = xeDocumentNode.SelectSingleNode("//VirtualMachineName");
                XmlNode xnControlPort = xeDocumentNode.SelectSingleNode("//ControlPort");
                XmlNode xnODDController = xeDocumentNode.SelectSingleNode("//ODD-Controller");
                XmlNode xnODDPort = xeDocumentNode.SelectSingleNode("//ODD-Port");
                XmlNode xnODDDevice = xeDocumentNode.SelectSingleNode("//ODD-Device");
                XmlNode xnHDDController = xeDocumentNode.SelectSingleNode("//HDD-Controller");
                XmlNode xnUpdateInterval = xeDocumentNode.SelectSingleNode("//MonitorUpdateInterval");

                _VBoxManagePath = xnVBoxManagePath.InnerText;
                _VBoxGuestName = xnVirtualMachineName.InnerText;
                _ControlPort = xnControlPort.InnerText;
                _ODDController = xnODDController.InnerText;
                _ODDPort = xnODDPort.InnerText;
                _ODDDevice = xnODDDevice.InnerText;
                _HDDController = xnHDDController.InnerText;

                try
                {
                    _UpdateInterval = int.Parse(xnUpdateInterval.InnerText);
                }
                catch (Exception ex)
                {
                    _UpdateInterval = 15;
                }

            }
            catch (Exception ex)
            {
                oServiceEvents.WriteEntry("Unable to load Settings from: \"" + getServiceBaseDir() + "config.xml\": " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }

            return true;
        }

        /// <summary>
        /// Get Base-Directory of the service-executable.
        /// </summary>
        /// <returns>(string) Base-Directory-Path.</returns>
        public static string getServiceBaseDir()
        {
            string sBaseDir = String.Empty;

            sBaseDir = System.Reflection.Assembly.GetEntryAssembly().Location.Replace("VBoxService.exe", "");
            sBaseDir = sBaseDir.Replace("VBoxServiceMonitor.exe", "");

            return sBaseDir;
        }
    }
}
